package com.fjw.controller;

import com.fjw.captcha.Captcha2;
import com.fjw.pojo.SysMenu;
import com.fjw.pojo.SysUser;
import com.fjw.service.PermsService;
import com.fjw.service.SysMenuService;
import com.fjw.service.SysUserService;
import com.fjw.utils.R;
import com.fjw.vo.SysUserVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Set;


@CrossOrigin(value = "http://localhost:8085")
@Controller
@RequestMapping("/sys")
public class SysUserController {


    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysMenuService sysMenuService;
    @Autowired
    private PermsService permsService;

    //用户登录  路径: http://ssx:8080/sys/login
    //返回一个{code:0}
    @PostMapping("/login")
    @ResponseBody
    public R login(@RequestBody SysUserVo sysUserVo,HttpSession session){
        //接受参数 并且登录
        System.out.println("前端输入的用户名"+sysUserVo.getUsername());
        System.out.println("前端输入的验证码"+sysUserVo.getCaptcha());

        //验证验证码是否正确

        String captcha1 = (String) session.getAttribute("captcha");
        System.out.println("session中的验证码"+captcha1);
        if (!captcha1.equalsIgnoreCase(sysUserVo.getCaptcha())){
            return R.error("验证码错误lllll");
        }
        //判断下是否点了记住我
        Boolean RememberMe=false;
        if (sysUserVo.getRememberMe()!=null){
            RememberMe = true;
        }
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(sysUserVo.getUsername(),sysUserVo.getPassword(),RememberMe);
        SecurityUtils.getSubject().login(usernamePasswordToken);
        return R.ok();
    }
    //配置验证码
    @GetMapping("/captcha")
    public void captcha(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Captcha2.generateCaptcha(request,response);
    }

    //在登录界面 显示用户名/sys/user/info 返回一个SysUser
    @GetMapping("/user/info")
    @ResponseBody
    public R info(){
        //从当前对象中获得用户名
        String username = (String) SecurityUtils.getSubject().getPrincipal();
        SysUser sysUser = sysUserService.queryByUsername(username);
        return R.ok().put("user", sysUser);
    }

    //在主界面中获得左边的菜单栏
    @GetMapping("/menu/user")
    @ResponseBody
    public R user(){
        //从当前对象中获得用户名
        String username = (String) SecurityUtils.getSubject().getPrincipal();
        //根据用户名去查询权限信息
        Set<SysMenu> menuList = sysMenuService.findByUsername(username);
        Set<String> perms = permsService.findAllPerms(username);
        return R.ok().put("menuList",menuList).put("permissions",perms);
    }

    //退出系统/logout
    @GetMapping("/logout")
    @ResponseBody
    public R logout(){
        SecurityUtils.getSubject().logout();
        return R.ok();
    }

    //点击用户管理跳转到用户界面





    //修改密码 sys/user/password"
    @PostMapping("/user/password")
    @ResponseBody
    public R updatePassword(String password,String newPassword){
        System.out.println("原密码"+password);
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipal();
        //判断原密码是正确
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username,password);

        try {
            subject.login(usernamePasswordToken);
        } catch (AuthenticationException e) {
            e.printStackTrace();
            R.error("修改密码失败,原密码不正确");
        }
        //判断原密码是否与新密码相同
        if (password.equals(newPassword)){
            R.error("修改密码失败,原密码不能与新密码相同");
        }


        //说明能登录成功是正确的密码
        System.out.println("新密码"+newPassword);
        Integer i = sysUserService.updatePassword(username,newPassword);
        if (i==1){
            return R.ok();
        }
        return R.error("修改密码失败");
    }

}
